package com.osi.utils;

import com.osi.bean.Logininfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class UserContext {
    private static final String CURRENT_USER_IN_SESSION = "logininfo";
    /**
     * 得到session
     */
    private static HttpSession getSession(){
        //SpringMVC获取session的方式通过RequestContextHolder
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    }
    /**
     * 设置当前用户到session中
     */
    public static void putCurrebtUser(Logininfo currentUser) {
       getSession().setAttribute(CURRENT_USER_IN_SESSION, currentUser);
    }
    /**
     * 获取当前用户
     */
    public static Logininfo getCurreentUser() {
        return (Logininfo) getSession().getAttribute(CURRENT_USER_IN_SESSION);
    }
}
